package com.goldman.aplt.base.handel;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.goldman.aplt.util.CommUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.Instant;
import java.util.Date;

@Slf4j
public class MybatisHandler implements MetaObjectHandler {
    /**
     * 获取当前交易的用户，为空返回默认system
     *
     * @return CurrentUsername
     */
    private String getCurrentUsername() {
        return CommUtil.getTrxnEnvs().getApp_uid();
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("dataCreateUser", CommUtil.isNull(metaObject.getValue("dataCreateUser")) ? getCurrentUsername() : metaObject.getValue("dataCreateUser"), metaObject);
        this.setFieldValByName("dataCreateTime", CommUtil.getComputerDateTime(), metaObject);
        this.setFieldValByName("dataVersion",1, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("dataUpdateUser", CommUtil.isNull(metaObject.getValue("dataUpdateUser")) ? getCurrentUsername() : metaObject.getValue("dataUpdateUser"), metaObject);
        this.setFieldValByName("dataUpdateTime", CommUtil.getComputerDateTime(), metaObject);
        this.setFieldValByName("dataVersion", (int)metaObject.getValue("dataVersion") + 1, metaObject);
    }
}